#include "comm.hpp"

int main()
{
    vector<int> v;
    //以写的方式打开管道，形成通信
    string s = PATH;
    for(int i = 0; i < PROCESS_NUM; ++i)
    {
        string namePipe = s + to_string(i);
        int wfd = open(namePipe.c_str(), O_WRONLY);
        if(wfd < 0)
            cout << strerror(errno) << endl;
        v.push_back(wfd);
    }

    while (1)
    {
        int subIdx;
        int taskIdx;
        cout << "给几号进程发任务：";
        cin >> subIdx;
        cout << "执行那个任务：";
        cin >> taskIdx;
        if (subIdx > 0 && subIdx <= v.size())
        {
            sendTask(v[subIdx - 1], taskIdx);  
            cout << "客户端发送任务 " << taskIdx << " 到子进程 " << subIdx << endl;
        }
        else
        {
            cout << "无效的进程号：" << subIdx << endl;
        }
        //发送给对应的管道
        //sendTask(v[subIdx],taskIdx);
        //cout << "客户端发送任务 " << taskIdx << " 到子进程 " << subIdx << endl;

    }
    //发送结束，关闭发送端。
    for(auto& e : v)
    {
        close(e);
    }
    return 0;
}